package com.flyme.util.test;

import com.flyme.common.poi.util.WordTemplateData;
import com.flyme.common.poi.util.WordTemplateUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

/**
 * Created by xinxingegeya on 2016/12/27.
 */
public class AppMain2 {

    public static void main(String args[]) throws IOException {

        WordTemplateData data = new WordTemplateData();
        data.addPlainTextData("saleNo", "23232323232323");
        data.addTableData("agencyName", "hello wrold");
        data.addTableData("agencyAddress", "hello world1");
        data.addTableData("agencyTel", "hello world2");
        data.addTableData("agencyContact", "hello world3");
        data.addTableData("deptName", "hello world4");
        data.addTableData("deptAddress", "hello world5"); //门店地址
        data.addTableData("deptTel", "hello world6"); //门店负责人电话
        data.addTableData("deptContact", "hello world7"); //门店负责人
        data.addTableData("vehicleModel", "hello world8");
        data.addTableData("bodyColor", "asfsadfsaf");
        data.addTableData("innerColor", "sadfsafa22");
        data.addTableData("guidePrice", "asdfsafdasf");
        data.addTableData("purchasePrice", "adfasfw2efsdafasdf");
        data.addTableData("purchaseNum", "asdfasfdwqef");
        data.addTableData("purchaseTotalPrice", "e2323232323");
        data.addTableData("purchaseTotalPriceCN", "savxzcvxzcvxzcv");
        data.addTableData("beforeContractPrice", "asdfsaf");
        data.addTableData("beforeContractPriceCN", "asdfafewf");
        data.addTableData("afterPickupCarPrice", "sadzxczcvzv");
        data.addTableData("afterPickupCarPriceCN", "ksdflhsafdhashfdio");
        data.addTableData("agencyBankAccount", "345678uio5678");
        data.addTableData("agencyBankName", "niasdfa");


        String templatePath = "买买车-4S车源-车辆采购合同-修订20161226.docx";
        XWPFDocument document = WordTemplateUtil.generateWordFromTemplate(templatePath, data);

        FileOutputStream fileOutputStream = null;
        URL path = Thread.currentThread().getContextClassLoader().getResource("");

        if (path == null) {
            return;
        }

        try {
            File file = new File(path.getPath(), "hello2.docx");
            fileOutputStream = new FileOutputStream(file);

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            if (document != null) {
                document.write(out);
            } else {
                return;
            }

            byte[] array = out.toByteArray();
            for (int i = 0; i < array.length; i++) {
                fileOutputStream.write(array[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }

    }
}
